define(["angularAMD"], function (angularAMD) {
    angularAMD.service('QT06_LoadTemplateService', ['$state','$rootScope', '$filter', '$stateParams', 'config', '$mdDialog', 'MMWService', '$timeout', '$q',
        function ($state, $rootScope, $filter, $stateParams, config, $mdDialog, $MMWService, $timeout, $q) {

            var service = this;
            
            service.init = (option, call_back) => {
                openDialog(option, call_back);
            };
            
            // 20220721 modify by Alan for#113392 : 卡控不刪除當前模板
            function openDialog(option, call_back) {
                $mdDialog.dialog(
                    "module/moduleQT/program/QT06/dialog/QT06_LoadTemplateService.html",
                    function (dialog) {
                        return {
                            css_url: "module/moduleQT/program/QT06/dialog//QT06_LoadTemplateService.css",
                            templateList: [],
                            displayModelName: option.modelName,
                            selectModelName: '',
                            traceNameList: [],
                            beforeShown: async () => {
                                // 从模板记录表中取出当前账号与界面的模板列表。
                                const resp = await loadTemplate();
                                
                                if (resp.TemplateList && resp.TemplateList.length > 0) {
                                    $timeout(async () => {
                                        dialog.templateList = resp.TemplateList;
                                    });
                                }
                            },
                            back: () => dialog.hide(),
                            delete: (template) => {
                                if(template.MODELNAME === dialog.displayModelName) {
                                    $rootScope.showAlert('無法刪除使用中模板');
                                    return;
                                } else {
                                    $rootScope.showConfirm(
                                        $filter("translate")("QT_standard.common.loadModule.msg.whether_delete_this_module"),
                                        async (confirmDialog) => {
                                            confirmDialog.hide();
                                            
                                            // 20220719 modify by Alan for#113392 : 修正預設模板觸發時機
                                            await deleteTemplate(template.MODELNAME);
                                            const resp = await loadTemplate();
                                            $timeout(async () => {
                                                dialog.templateList = resp.TemplateList;
                                                if (resp.TemplateList && resp.TemplateList.length === 0) {
                                                    dialog.selectModelName = '';
                                                    await setDefaultTemplate(option.currentTemplate);
                                                    call_back && call_back(dialog);
                                                }
                                            });
                                            
                                        }
                                    );
                                }
                            },
                            confirm: async () => {
                                dialog.hide();

                                if(!dialog.selectModelName) {
                                    return;
                                }

                                const resp = await changeTemplate(dialog.selectModelName);
                                if(resp.TRACENAMEList && resp.TRACENAMEList.length > 0){
                                    dialog.traceNameList = resp.TRACENAMEList;
                                    dialog.traceNameList.forEach(element => {
                                        element.TRACENO = element.MODELSHEET;
                                    });
                                    call_back && call_back(dialog);
                                }

                            },
                        };
                    }
                );
            };

            const service_QTP_Step3 =  (content) => {
                return new Promise(async (resolve, reject) => {
                    let result;
                    $MMWService.sendToServer({
                        uri: "ServicesSTD.Module_LEAN.QTP_Step3",
                        content: content,
                        success: (data) => (result = resolve(data)),
                    });

                    return result;
                });
            };
            
            const loadTemplate = async () => {
                const content = { Name: "GetTemplateList" };
                const resp = await service_QTP_Step3(content);
                return resp
            }

            const deleteTemplate = async (modelName) => {
                const content = { Name: "DeleteTemplate", ModelName: modelName };
                const resp = await service_QTP_Step3(content);
                return resp
            }

            const changeTemplate = async (modelName) => {
                const content = { Name: "ChangeTemplate", ModelName: modelName };
                const resp = await service_QTP_Step3(content);
                return resp
            }

            const setDefaultTemplate = async (template) => {
                const content = {
                    Name: "IniTemplate",
                    Data: template,
                };
                const resp = await service_QTP_Step3(content);
                return resp
            }
        }
    ])
});
